package com.edu.framework.websocket.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.message.timetable.TimetableVO;
import com.edu.framework.websocket.message.timetable.TimetableVO.TableVO;
import com.edu.system.domain.Student;
import com.edu.system.domain.TClass;
import com.edu.system.domain.TClassExt;
import com.edu.system.domain.TClassroom;
import com.edu.system.domain.TLessonTimetable;
import com.edu.system.domain.dto.TimetableDO;
import com.edu.system.mapper.TClassExtMapper;
import com.edu.system.mapper.TLessonTimetableMapper;
import com.edu.system.service.IStudentService;
import com.edu.system.service.ITClassService;
import com.edu.system.service.ITClassroomService;
import com.edu.system.service.StudentClassExtService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

/**
 * @program: xxxx
 * @description:
 * @author: zlh
 * @create: 2024-01-06 18:20
 **/
@Slf4j
@Component
public class TimetableService {

  @Resource
  private ITClassService itClassService;
  @Resource
  private IStudentService iStudentService;
  @Resource
  private TLessonTimetableMapper tLessonTimetableMapper;
  @Resource
  private StudentClassExtService studentClassExtService;
  @Resource
  private TClassExtMapper tClassExtMapper;
  @Resource
  private ITClassroomService itClassroomService;

  private static final String[] NUM_ARR = new String[]{"第一节", "第二节", "第三节", "第四节", "第五节", "第六节", "第七节", "第八节", "第九节", "第十节", "第十一节"};


  public TimetableVO getTimetable(Long roomId, String cardNo, Date lessonDate){
    String title = null;
    TimetableDO tableDO = new TimetableDO();
    Map<String, String> locationIdNameMap = new HashMap<>();
    if (StrUtil.isNotBlank(cardNo)){
      Student student = iStudentService.selectStudentByCardNo(cardNo);
      // 查学生下所有班级code
      List<String> codes = studentClassExtService.selectClassCodeByEid(student.getStuNo());
      tableDO.setClassCodes(codes);
      title = student.getName();

      locationIdNameMap = itClassroomService.selectLocationIdNameMap();
    } else {
      TClass tClass = itClassService.selectTClassByRoomId(roomId);
      TClassroom classroom = itClassroomService.selectTClassroomById(roomId);
      if (classroom == null){
        log.error("获取课表 教室不存在，roomId:{}", roomId);
        return TimetableVO.builder().build();
      }
      // 查班级对应的code
      tableDO.setLocationId(classroom.getCode());
      title = Optional.of(tClass).map(TClass::getRoomName).orElseGet(() -> "");
    }

    tableDO.setStartLessonDate(DateUtil.formatDate(lessonDate));
    tableDO.setEndLessonDate(DateUtil.formatDate(lessonDate));
    List<TLessonTimetable> list = tLessonTimetableMapper.selectTimetableList(tableDO);

    List<TableVO> res = new ArrayList<>();
    Set<String> exist = new HashSet<>();
    int n = 0;
    for (TLessonTimetable d : list) {
      if (exist.contains(d.getStartTime())) {
        continue;
      }
      exist.add(d.getStartTime());

      // 教室名称
      String cName = StrUtil.isNotBlank(cardNo) ?
          locationIdNameMap.getOrDefault(d.getLocationId(), "")
          : d.getClassName();
      res.add(TableVO.builder()
          .num(this.getNumStr(n++))
          .startEnd(d.getStartTime() + "~" + d.getEndTime())
          .start(d.getStartTime())
          .end(d.getEndTime())
          .lesson(d.getLessonName())
          .teacher(d.getTeacherName())
          .classroom(cName)
          .build());
    }

    return TimetableVO.builder()
        .title(title + " 课程表")
        .timeTable(res)
        .build();

  }

  private String getNumStr(Integer n) {
    if (n >= NUM_ARR.length) {
      return "";
    }
    return NUM_ARR[n];
  }

}
